package com.javaweb.blogcite.controller;

import com.javaweb.blogcite.aspect.JwtCheck;
import com.javaweb.blogcite.common.ResponseResult;
import com.javaweb.blogcite.util.UploadUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class ImageUploadController {
    @JwtCheck
    @PostMapping("/api/upload")
    public ResponseResult<String> uploadImage(@RequestParam("file") MultipartFile file) {
        ResponseResult<String> responseResult = new ResponseResult<>();
        responseResult.setCode(0);
        responseResult.setMsg("success");
        String url = null;

        try {
            url = UploadUtil.uploadImage(file);
        } catch (IOException e) {
            e.printStackTrace();
            responseResult.setCode(500);
            responseResult.setMsg("上传失败！");
            return responseResult;
        }
        responseResult.setData(url);
        return responseResult;
    }
}

